package com.xxx.logger;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/8/17 14:31
 * @Copyright 博为峰
 */
@Component("logger")
@Aspect
public class Logger {

    @Pointcut("execution(* com.xxx.service.*.*(..))")
    public void pt(){
    }

    @Before("pt()")
    public void beforeLog() {
        System.out.println("前置通知->在目标方法调用之前执行");
    }
    @AfterReturning("pt()")
    public void afterReturnLog() {
        System.out.println("后置通知->在目标方法调用之后执行");
    }

    @AfterThrowing("pt()")
    public void afterThrowingLog() {
        System.out.println("异常通知->在目标方法抛出异常时执行");
    }

    @After("pt()")
    public void afterLog() {
        System.out.println("最终通知->在目标方法是否正常执行都会在其后执行");
    }

    @Around("pt()")
    public void aroundLog(ProceedingJoinPoint point) {
        try {
            System.out.println("环绕-前置通知->在目标方法调用之前执行");
            point.proceed();//调用目标方法
            System.out.println("环绕-后置通知->在目标方法调用之后执行");
        } catch (Throwable throwable) {
            System.out.println("环绕-异常通知->在目标方法抛出异常时执行");
            throwable.printStackTrace();
        } finally {
            System.out.println("环绕-最终通知->在目标方法是否正常执行都会在其后执行");
        }
    }
}
